package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.adapter.SZJAdapter
import com.example.xinhuazidian.bean.Shuimotangshi
import com.example.xinhuazidian.databinding.ActivityShuimotangshiXiangqingBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 水墨唐诗详情
 */
class ShuimotangshiXiangqingActivity : AppCompatActivity() {

    private lateinit var binding: ActivityShuimotangshiXiangqingBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityShuimotangshiXiangqingBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
        initData()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()
    }

    @SuppressLint("SetTextI18n")
    private fun initData() {
        val beanStr = intent.getStringExtra("bean")
        if (beanStr != null) {
            val bean = Gson().fromJson(beanStr, Shuimotangshi::class.java)

            if (bean.author != null) {
                binding.tvTitle.text = "${bean.title} · ${bean.author}"
            } else {
                binding.tvTitle.text = bean.title
            }

            initAdapter(bean.paragraphs)

            if (bean.prologue == null || "" == bean.prologue) {
                binding.tvPrologue.visibility = View.GONE
                binding.tvPrologue.text = "介绍：无"
            } else {
                binding.tvPrologue.visibility = View.VISIBLE
                binding.tvPrologue.text = "介绍：${bean.prologue}"
            }
        }
    }

    private fun initAdapter(list: MutableList<String>) {
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val szjAdapter = SZJAdapter(list)
        binding.rv.adapter = szjAdapter
    }
}